package question

// 50. Pow(x, n)
func myPow(x float64, n int) float64 {
	if n < 0 {
		return 1.0 / myPow(x, -1*n)
	} else if n == 0 {
		return 1
	}
	//根据n的二进制来决定相乘
	res := 1.0
	temp := x
	for n > 0 {
		if n%2 == 1 {
			res *= temp
		}
		temp *= temp
		n /= 2
	}
	return res
}
